package com.woniu.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.bean.R;
import com.woniu.car.bean.TReceipt;
import com.woniu.car.service.ITReceiptService;
import com.woniu.need.bean.TNeed;
import com.woniu.need.service.ITNeedService;
import com.woniu.order.bean.*;
import com.woniu.order.service.*;
import com.woniu.order.util.RedisSeachUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-08
 */

@CrossOrigin
@RestController
@RequestMapping("/tOrder")
public class TOrderController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private ITOrderService itOrderService;
    @Autowired
    private ITOrderbasicinfoService orderbasicinfoService;
    @Autowired
    private ITGoodsService itGoodsService;
    @Autowired
    private ITCostService itCostService;
    @Autowired
    private ITAddrinfoService itAddrinfoService;
    @Autowired
    private IFAddrinfoService ifAddrinfoService;
    @Autowired
    private RedisSeachUtil redisSeachUtil;
    @Autowired
    private ITLogisticsService logisticsService;
    @Autowired
    private ITReceiptService itReceiptService;
    @Autowired
    private ITNeedService itNeedService;
    @Autowired
    private INewMsgService iNewMsgService;

    @GetMapping("getmsg")
    public R getmsg(){
        NewMsg byId = iNewMsgService.getById(1);
        Integer msgNum = byId.getMsgNum();
        System.out.println("msgNum = " + msgNum);
        R r =new R();
        Map map=new HashMap();
        map.put("msg",msgNum);
        r.setData(map);
        return r;
    }


    @GetMapping("newmsg")
    public R changemsg(){
        NewMsg byId = iNewMsgService.getById(1);
        Integer msgNum = byId.getMsgNum();
        if(msgNum==0){
            return null;
        }
        byId.setMsgNum(msgNum-1);
        boolean b = iNewMsgService.updateById(byId);
        if(b){
            R r = R.OK();
            return r;
        }
        return null;
    }

    @GetMapping("/look/{id}")
    public R lookorder(@PathVariable Integer id){

        QueryWrapper qw=new QueryWrapper();
        qw.eq("order_id", id);
        TOrderbasicinfo basi = orderbasicinfoService.getOne(qw);
        TCost tCost = itCostService.getOne(qw);
        TGoods tGoods = itGoodsService.getOne(qw);
        TAddrinfo tAddrinfo = itAddrinfoService.getOne(qw);
        FAddrinfo fAddrinfo = ifAddrinfoService.getOne(qw);
        TLogistics tLogistics = logisticsService.getOne(qw);
        Map map=new HashMap<>();
        map.put("basi",basi);
        map.put("tCost",tCost);
        map.put("tGoods",tGoods);
        map.put("tAddrinfo",tAddrinfo);
        map.put("fAddrinfo",fAddrinfo);
        map.put("tLogistics",tLogistics);
        R r=new R();
        r.setData(map);
        return r;
    }

    @GetMapping
    public R findall(Integer currentPage, Integer pageSize,Integer number,String username){
        R r = itOrderService.selectall(currentPage, pageSize,number,username);
        return r;
    }
    @GetMapping("submit")
    public R saveAll() throws JsonProcessingException {
        R r=null;
        List<TOrder> list = itOrderService.list();
        Integer orderId = list.get(list.size() - 1).getOrderId();
        orderId+=1;
        OverAll redis = redisSeachUtil.getRedis(orderId);
        if(redis==null){
            r.setMsg("请先录入订单信息");
            return r;
        }
        Address address = redis.getAddress();
        TCost tCost = redis.gettCost();
        TGoods tGoods = redis.gettGoods();
        TLogistics tLogistics = redis.gettLogistics();
        TOrderbasicinfo tOrderbasicinfo = redis.gettOrderbasicinfo();
        TAddrinfo tAddrinfo=address.gettAddrinfo();
        FAddrinfo fAddrinfo=address.getfAddrinfo();
        if(address==null){
            r.setMsg("请录入地址信息");
            return r;
        }
        if(tCost==null){
            r.setMsg("请录入费用信息");
            return r;
        }
        if(tGoods==null){
            r.setMsg("请录入货物信息");
            return r;
        }
        if(tOrderbasicinfo==null){
            r.setMsg("请录入基础信息");
            return r;
        }
        boolean save = orderbasicinfoService.save(tOrderbasicinfo);
        boolean save1 = itGoodsService.save(tGoods);
        boolean save2 = itCostService.save(tCost);
        boolean save3 = itAddrinfoService.save(tAddrinfo);
        boolean save4 = ifAddrinfoService.save(fAddrinfo);
        boolean save6 = logisticsService.save(tLogistics);
        TOrder tOrder=new TOrder();
        tOrder.setOrderId(tOrderbasicinfo.getOrderId());
        tOrder.setUsername(tOrderbasicinfo.getUsername());
        tOrder.setTypeName(tOrderbasicinfo.getTypeName());
        tOrder.setTranType(tGoods.getTranType());
        tOrder.setOrderTime(tOrderbasicinfo.getReceiveTime());
        tOrder.setBeginCity(tAddrinfo.getCity());
        tOrder.setEndCity(fAddrinfo.getCity());
        boolean save5 = itOrderService.save(tOrder);
        NewMsg byId = iNewMsgService.getById(1);
        Integer msgNum = byId.getMsgNum();
        byId.setMsgNum(msgNum+1);
        iNewMsgService.updateById(byId);
        TNeed tNeed=new TNeed();
        tNeed.setOrderId(tOrderbasicinfo.getOrderId());
        tNeed.setLogId(tLogistics.getLogId());
        tNeed.setGoodsId(tGoods.getGoodsId());
        tNeed.setTypeName(tOrderbasicinfo.getTypeName());
        tNeed.setTranType(tGoods.getTranType());
        tNeed.setUsername(tOrderbasicinfo.getUsername());
        tNeed.setOrderTime(tOrderbasicinfo.getReceiveTime());
        tNeed.setRoute(tAddrinfo.getCity()+"-"+fAddrinfo.getCity());
        tNeed.setGoodsName(tGoods.getGoodsName());
        tNeed.setNumber(tGoods.getNumber());
        tNeed.setWeight((float)tGoods.getTotalWeight());
        tNeed.setBulk((float)tGoods.getTotalBulk());
        tNeed.setStatus(0);
        boolean save7 = itNeedService.save(tNeed);
        NewMsg byId2 = iNewMsgService.getById(2);
        Integer msgNum2 = byId.getMsgNum();
        byId.setMsgNum(msgNum2+1);
        iNewMsgService.updateById(byId2);
        
        TReceipt tReceipt=new TReceipt();
        tReceipt.setOrderId(tOrderbasicinfo.getOrderId());
        tReceipt.setReceiptDate(tOrderbasicinfo.getReceiveTime());
        tReceipt.setReceiptNum("h"+tOrderbasicinfo.getOrderId());
        tReceipt.setUsername(tOrderbasicinfo.getUsername());
        boolean save8 = itReceiptService.save(tReceipt);
        NewMsg byId3 = iNewMsgService.getById(3);
        Integer msgNum3 = byId.getMsgNum();
        byId.setMsgNum(msgNum3+1);
        iNewMsgService.updateById(byId3);
        if(save5){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;
    }

}
